package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import org.joda.time.DateTime;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.AdvertDAO;
import com.hcrazy.manchester.app.jdbc.RentDAO;
import com.hcrazy.manchester.app.pojo.Advert;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Rent;
import com.hcrazy.manchester.app.utils.Constant;

public class RentService {

	private RentDAO rentDAO;
	
	public RentDAO getRentDAO() {
		return rentDAO;
	}

	public void setRentDAO(RentDAO rentDAO) {
		this.rentDAO = rentDAO;
	}
	
	private AdvertDAO advertDAO;
	
	public AdvertDAO getAdvertDAO() {
		return advertDAO;
	}

	public void setAdvertDAO(AdvertDAO advertDAO) {
		this.advertDAO = advertDAO;
	}

	public ErrorMsg addOrUpdateRent(Rent rent) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		//添加发布时间
		DateTime dt = new DateTime();
		rent.setPublishDate(dt.toString("yyyy-MM-dd HH:mm:ss"));
		//如果有效时间为空，则添加默认，默认为30天
		if(null == rent.getPublishValidateDate() || "".equals(rent.getPublishValidateDate())){
			DateTime validateDt = dt.plusDays(30);
			rent.setPublishValidateDate(validateDt.toString("yyyy-MM-dd"));
		}
		
		
		long rentId = 0;
		
		if(rent.getRentId()>0){
			rent.setPraiseCount(rent.getPraiseCount() + rent.getPraiseBaseCount());
			rent.setCommentCount(rent.getCommentCount() + rent.getCommentBaseCount());
			rentDAO.updateRent(rent);
			rentId = rent.getRentId();
		}else{
			if(null != rent && rent.getPraiseBaseCount() > 0){
				rent.setPraiseCount(rent.getPraiseBaseCount());
			}
			if(null != rent && rent.getCommentBaseCount() > 0){
				rent.setCommentCount(rent.getCommentBaseCount());
			}
			rentId = rentDAO.addRent(rent);
		}
		
		if(rent.getAdvertStr() != null && !rent.getAdvertStr().equals("")){
			Gson gson = new Gson();
			Type type = new TypeToken<Advert>(){}.getType();
			Advert ad = gson.fromJson(rent.getAdvertStr(),type);
			if(null != ad && ad.getAdContent() != null && !ad.getAdContent().equals("")){
				advertDAO.deleteAdvert(rentId,Constant.COMM_RENT_INFO_MODULE);
				
				ad.setModuleId(rentId);
				ad.setModuleType(Constant.COMM_RENT_INFO_MODULE);
				advertDAO.addAdvert(ad);
			}
			
		}
		
		return msg;
	}
	
	public PageModel getRents(PageModel model,int theType,int rentType,int houseType,String zipCode,String address,
									float startArea,float endArea,float startPrice,float endPrice,
									String title,String linkName,String phone,String startPublishDate,String endPublishDate,int status) throws Exception{
		return rentDAO.getPagedInfos(model, theType,rentType, houseType, zipCode, address, startArea, endArea, startPrice, endPrice, title, linkName, phone, startPublishDate, endPublishDate, status);
	}
	
	public PageModel getRents(PageModel model,String keywords,long userId,int theType,
			int rentType,int houseType,int status,int priceRange,int priceStart,int priceEnd,int roomRange) throws Exception{
		PageModel pageModal = rentDAO.getPagedRents(model, keywords,userId,theType, rentType, houseType, status, priceRange, priceStart, priceEnd, roomRange);
		if(theType == Constant.RENT_TYPE_FOR_RENT){
			if(null != pageModal && null != pageModal.getResult() && !"".equals(pageModal.getResult())){
				for(Object rent_:pageModal.getResult()){
					Rent rent = (Rent)rent_;
					rent.setAdvert(null);
				}
			}
		}
		return pageModal;
	}
	
	public Rent getInfoById(long rentId) throws Exception{
		Rent rent = rentDAO.getById(rentId);
		Advert advert = advertDAO.getAdvert(rentId,Constant.COMM_RENT_INFO_MODULE);
		if(null != advert)
			rent.setAdvert(advert);
		return rent;
	}
	
	public void delRent(long rentId) throws Exception{
		rentDAO.delRent(rentId);
	}
	
	public void delRent(long rentId,long userId) throws Exception{
		//判断该信息是否属于当前用户
		Rent r = rentDAO.getById(rentId);
		if(null != r && r.getUserId() > 0 && r.getUserId() == userId){
			rentDAO.delRent(rentId);
		}
		
	}
	
	public void setViews(long id) throws Exception{
		//获取对象
		Rent secondary = rentDAO.getById(id);
		long views = secondary.getViews()+1;
		//计数+1
		rentDAO.setViews(views,id);
	}
	
	public List<Rent> getRents(int size) throws Exception{
		return rentDAO.getRents(size);
	}
}
